// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    freight:"0.00",
    packing:"0.00",
    address:"苏州市吴中区989文化创意园2幢401",
    cart:false,//购物车列表
    foodBg:false,//透明背景
    checkstyle:'',
    winHeight: 300,
    categoryList:[
      {
        name:"热销",
        id:"0",
        Food:[
          {
            name:'火腿肉丝',
            sold: 20,
            number:0,
            price:25,
            FoodId: 0
          },
          {
            name: "鱼香茄子1",
            price:20,
            sold: 20,
            number: 0,
            FoodId: 1
          }
        ]
      },
      {
        name:"炒菜",
        id:"1",
        Food:[
          {
            name:'青椒肉丝',
            sold: 20,
            number:0,
            price:25,
            FoodId: 2
          },{
            name:'青椒鸡蛋',
            sold: 20,
            number:0,
            price:25,
            FoodId: 3
          }
        ]
      },
      {
        name:"套餐",
        id:"2",
        Food:[
          {
            name:'青椒肉丝盖饭',
            sold: 20,
            number:0,
            price:25,
            FoodId: 4
          },{
            name:'地三鲜盖饭',
            sold: 20,
            number:0,
            price:25,
            FoodId: 5
          },{
            name:'麻婆豆腐盖饭',
            sold: 20,
            number:0,
            price:25,
            FoodId: 6
          }
        ]
      },
      {
        name:"荤菜",
        id:"3",
        Food:[
          {
            name:'小炒肉',
            sold: 20,
            number:0,
            price:25,
            FoodId: 7
          },{
            name:'铁板鱿鱼',
            sold: 20,
            number:0,
            price:25,
            FoodId: 8
          },{
            name:'辣炒牛肉',
            sold: 20,
            number:0,
            price:25,
            FoodId: 9
          }
        ]
      },
      {
        name:"蔬菜",
        id:"4",
        Food:[
          {
            name:'蚝油生菜',
            sold: 20,
            number:0,
            price:25,
            FoodId: 10
          },{
            name:'上海青蔬菜',
            sold: 20,
            number:0,
            price:25,
            FoodId: 11
          }
        ]
      },
      {
        name:"主食",
        id:"5",
        Food:[
          {
            name:'米饭',
            sold: 20,
            number:0,
            price:2,
            FoodId: 12
          },{
            name:'面条',
            sold: 20,
            number:0,
            price:5,
            FoodId: 13
          }
        ]
      },
    ],
    PayList: [],
    scrollTopId:'',
    heightArr: [],
    forindex: true,
    zindex: 0,
    carNumber: 0, //购物车数量
    allPice: 0,//总价格
  },
 
  //点击购物车
  checkList(){
    var that = this;
    this.setData({
      cart:!this.data.cart,
      foodBg:!this.data.foodBg
    })
    that.Relist()
  },
  //点击菜品类型
  choosecategory(e){
    console.log(e)
    var id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    console.log(id)
    this.setData({
      checkstyle:index,
      scrollTopId:id
    })
    console.log(this.data.scrollTopId)
  },

  //滑动触动
  onScroll(e) {
    console.log(123)
    console.log(e)
    var that = this;
    var h = 0;
    var heightArr = [];
    let show = that.data.forindex;
    let scrollTop = e.detail.scrollTop;
    let scrollArr = that.data.heightArr;
    console.log(scrollTop)

    wx.createSelectorQuery().selectAll('.only').boundingClientRect(function(rect) {//selectAll会选择所要含有该类名的盒子
    }).exec(function(res){
      res[0].forEach((item) => {
        h += item.height-100;
        heightArr.push(h);
      })
      that.setData({
        heightArr: heightArr
      })
      console.log(234234)
      console.log(that.data.heightArr)
    });

    for (let i = 0; i < scrollArr.length; i++) {
      if (scrollTop >= 0 && scrollTop < scrollArr[0]) {
        if (show) {
          that.setData({
            checkstyle: 0,
            forindex: false,
            zindex: 0
          })
          return
        }
      } else if (scrollTop >= (scrollArr[i - 1]) && scrollTop < scrollArr[i]) {
        that.setData({
          checkstyle: i,
          forindex: true,
          zindex: 1
        })
      }
    }
  },

  // //点击减少菜品
  RmTotal(e){
    var that = this
    var index = e.currentTarget.dataset.itemIndex;//组内顺序
    var parentIndex = e.currentTarget.dataset.parentindex; //组顺序
    this.data.categoryList[parentIndex].Food[index].number--; //数量减少
    var num = this.data.categoryList[parentIndex].Food[index].number;
    var name = this.data.categoryList[parentIndex].Food[index].name;
    var sold = this.data.categoryList[parentIndex].Food[index].sold;
    var price = this.data.categoryList[parentIndex].Food[index].price;
    var FoodId = this.data.categoryList[parentIndex].Food[index].FoodId;
    var mark = 'a' + parentIndex + 'b' + index ;

    var obj = {
      parentIndex: parentIndex, 
      index: index,
      number: num,   
      name: name,
      index: index,
      sold: sold,
      price: price,
      FoodId:FoodId,
      mark: mark,  
    };
    console.log(e)
    var carArray1 = this.data.PayList.filter(item => item.mark != mark)
    carArray1.push(obj)
    this.setData({
      PayList:carArray1,
      categoryList:this.data.categoryList
    })
    that.Relist();
  },
  // 点击添加
  AddTotal:function(e){
    var that = this
    var index = e.currentTarget.dataset.itemIndex;//组内顺序
    var parentIndex = e.currentTarget.dataset.parentindex;//组顺序
    this.data.categoryList[parentIndex].Food[index].number++; //数量增加
    var num = this.data.categoryList[parentIndex].Food[index].number;
    var mark = 'a' + parentIndex + 'b' + index ;
    var price = this.data.categoryList[parentIndex].Food[index].price;
    var FoodId = this.data.categoryList[parentIndex].Food[index].FoodId;
    var name = this.data.categoryList[parentIndex].Food[index].name;
    var sold = this.data.categoryList[parentIndex].Food[index].sold;

    var obj = {
      number: num,
      name: name,
      index: index,
      sold: sold,
      price: price,
      FoodId:FoodId,
      mark: mark,
      parentIndex: parentIndex,      
    };
    console.log(e)
    var carArray1 = this.data.PayList.filter(item => item.mark != mark)
    carArray1.push(obj)
    this.setData({
      PayList: carArray1,
      categoryList:this.data.categoryList
    })
    // console.log(this.data.PayList)
    // console.log("购物车数量：")
    that.Relist();
  },

  //购物车数量
  Relist() {
    var that = this
    var allNumber = 0;
    var allPrice = 0
    for(let i =0; i<that.data.PayList.length;i++){
      allNumber += that.data.PayList[i].number;
      allPrice += that.data.PayList[i].number*that.data.PayList[i].price
    }
    that.setData({
      carNumber:allNumber,
      allPice:allPrice
    })    
  },

  //去结算页面
  toPay:function(){
    wx.navigateTo({
      url: '../account/account',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      PayList:this.data.PayList,
    });
    // console.log(123)
    console.log(this.data.winHeight)
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight: res.windowHeight - 200
        });
      }
    });
       
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})